package cn.tedu.medicalmanagementsystem.manager.controller;

import cn.tedu.medicalmanagementsystem.base.response.JsonResult;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.AppointmentAddParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.AppointmentDelParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.AppointmentListQuery;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.AppointmentUpdateParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.AppointmentVO;
import cn.tedu.medicalmanagementsystem.manager.service.ManagerAppointmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "预约管理模块")
@RequestMapping("/v1/appointment")
public class ManagerAppointmentController {

    @Autowired
    ManagerAppointmentService managerAppointmentService;

    @PostMapping("/add")
    @ApiOperation("创建预约记录")
    public JsonResult add(@RequestBody AppointmentAddParam appointmentAddParam) {
        log.debug("开始处理【添加预约】的请求，参数：{}", appointmentAddParam);

        // 调用业务层方法，执行添加操作
        AppointmentVO appointmentVO = managerAppointmentService.add(appointmentAddParam);
        return JsonResult.ok(appointmentVO);
    }

    @PostMapping("/update")
    @ApiOperation("修改预约记录")
    public JsonResult update(@RequestBody AppointmentUpdateParam appointmentUpdateParam) {
        log.debug("开始处理【修改预约】的请求，参数：{}", appointmentUpdateParam);

        AppointmentVO appointmentVO = managerAppointmentService.update(appointmentUpdateParam);
        return JsonResult.ok(appointmentVO);
    }
    @PostMapping("/delete")
    @ApiOperation("删除预约记录")
    public JsonResult delete(@RequestBody AppointmentDelParam appointmentDelParam) {
        log.debug("开始处理【删除预约】的请求，参数：{}", appointmentDelParam);
        managerAppointmentService.delete(appointmentDelParam);
        return JsonResult.ok();
    }
    //查询预约记录
    @GetMapping("/appointments")
    @ApiOperation("查询预约记录列表")
    public JsonResult list(){
        log.debug("开始处理【查询预约记录列表】的请求");
        List<AppointmentVO> list = managerAppointmentService.allAppointments();
        return JsonResult.ok(list);
    }
    //根据用户ID查询预约记录
    @GetMapping("/user/id/{userId}")
    @ApiOperation("根据用户ID查询预约记录")
    public JsonResult getByUserId(@PathVariable Long userId){
        log.debug("开始处理【根据用户ID查询预约记录】的请求，参数：{}", userId);
        List<AppointmentVO> list = managerAppointmentService.getByUserId(userId);
        return JsonResult.ok(list);
    }

    //根据预约用户姓名查询预约记录
    @GetMapping("/user/name/{realName}")
    @ApiOperation("根据预约用户姓名查询预约记录")
    public JsonResult getByRealName(@PathVariable String realName){
        log.debug("开始处理【根据预约用户姓名查询预约记录】的请求，参数：{}", realName);
        List<AppointmentVO> list = managerAppointmentService.getByRealName(realName);
        return JsonResult.ok(list);
    }

    //根据联系人姓名查询预约记录
    @GetMapping("/contactName")
    @ApiOperation("根据联系人姓名查询预约记录")
    public JsonResult getByContactRealName(@RequestParam String contactName) {
        // 添加日志查看接收到的参数
        log.info("Searching appointments with contact name: {}", contactName);

        List<AppointmentVO> list = managerAppointmentService.getByContactRealName(contactName);

        // 添加日志查看查询结果
        log.info("Found {} appointments", list.size());

        return JsonResult.ok(list);
    }



    //根据预约ID查询预约记录
    @GetMapping("/{id}")
    @ApiOperation("根据预约ID查询预约记录")
    public JsonResult getById(@PathVariable Long id){
        log.debug("开始处理【根据预约ID查询预约记录】的请求，参数：{}",id);
        System.out.println("id为:===="+id);
        AppointmentVO appointmentVO = managerAppointmentService.getById(id);
        return JsonResult.ok(appointmentVO);
    }

    @GetMapping("/select")
    public JsonResult select(AppointmentListQuery appointmentListQuery){
        log.debug("开始处理【根据预约ID查询预约记录】的请求，参数：{}",appointmentListQuery);
        List<AppointmentVO> list = managerAppointmentService.select(appointmentListQuery);
        return JsonResult.ok(list);
    }




}
